package chixing.day17.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 类全名：包名.类名
 * chixing.day17.reflect.ReflectDemo 在加载JVM中
 * 类的所有的结构{类名，父接口，父类，构造函数，属性，方法}
 * interface super
 */
public class ReflectDemo {
    public static void main(String[] args)
    {

    Class<MyRecord> clazz = MyRecord.class;
    String className = clazz.getName();
    System.out.println(className);
    Class<? super MyRecord> clazz1 = clazz.getSuperclass();
    System.out.println(clazz1);
    //父接口
        Class<?>[] clazz2 = clazz.getInterfaces();
        for (Class c : clazz2) {
            System.out.println(c);
        }
/*        Method[] methods =  clazz.getMethods();
        for(Method method:methods){
            String methodName = method.getName();
            System.out.println("1. 方法名：" + methodName);

            int modifiers = method.getModifiers();
            System.out.println("2. 方法的修饰符：" + modifiers);  // public(1) + static(8) + final (16)

            Class<?> returnType = method.getReturnType();
            System.out.println("3. 返回值类型：" + returnType);

            Class<?>[] parameterTypes = method.getParameterTypes();
            for(Class type:parameterTypes){
                System.out.print("4.方法参数列表的类型" + type);
            }
            if(methodName.equals("fun1")){
              MyRecord record = null;
                try {
                    record = clazz.getDeclaredConstructor().newInstance();
                } catch (InstantiationException e) {
                    throw new RuntimeException(e);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException(e);
                }
                try {
                    Object result = method.invoke(record, 1,2);
                    System.out.println("5. 方法返回值：" + result);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                }


            }
        }*/
        //获取MyRecord类中的字段
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields) {
            String fieldName = field.getName();
            System.out.println("字段名：" + fieldName);
        }
        Field field = null;
        MyRecord record1 = null;
        try {
            field = clazz.getDeclaredField("name");
            Class<?> type = field.getType();
            int modifiers = field.getModifiers();
            record1 = clazz.getDeclaredConstructor().newInstance();
            field.setAccessible(true);
            field.set(record1, "张三");
            Object value = field.get(record1);
            System.out.println("userName 赋值后的值是：" + value);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }


    }

}